package P3;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;

public class CrawlResult
{
    public String   Address         = " ";
    //public String   HostName        = " ";
    public String   Agent           = " ";
    public String   Status          = " "; // Uncrawled, crawl timed out, crawl failure, gnutella failure, crawled
    public String   Peers           = " ";
    public String   Leaves          = " ";
    public int      NumFiles        = 0;
    public int      AvgFileLength   = 0;
    public int      MinFileLength   = 0;
    public int      MaxFileLength   = 0;
    public String   FileExtensions  = " "; // Format is "extension:count;extension:count;..." example: "exe:153;txt:782;avi:9243;"
    
    public void InitializeCrawResult(int numFiles, int fileSize,
			String fileExtention) {
		NumFiles = numFiles;
		AvgFileLength = fileSize;
		MinFileLength = fileSize;
		MaxFileLength = fileSize;
		FileExtensions = fileExtention+":1;";
	}

    public String WriteToString()
    {
        StringBuilder output = new StringBuilder();
        
        output.append("CrawlResult\n");
        
        output.append(Address);
        output.append("\n");
        
        //output.append(HostName);
        //output.append("\n");
        
        output.append(Agent);
        output.append("\n");
        
        output.append(Status);
        output.append("\n");
        
        //output.append(Peers);
        //output.append("\n");
        
        //output.append(Leaves);
        //output.append("\n");
        
        output.append(NumFiles);
        output.append("\n");
        
        output.append(this.getAvgFileLength());
        output.append("\n");
        
        output.append(MinFileLength);
        output.append("\n");
        
        output.append(MaxFileLength);
        output.append("\n");
        
        output.append(FileExtensions);
        
        return output.toString();
    }
    
    public int getNumFiles() {
		return NumFiles;
	}
	public int getAvgFileLength() {
		if(getNumFiles() >0)
		
			return (AvgFileLength / getNumFiles());
		else
		
		return 0;
	}
	public int getMinFileLength() {
		return MinFileLength;
	}
	public int getMaxFileLength() {
		return MaxFileLength;
	}
	public String getFileExtensions() {
		return FileExtensions;
	}
	
	
	public void setAvgFileLength(int avgFileLength) {
		AvgFileLength += avgFileLength;
	}
	public void setMinMaxFileLength(int FileLength) {
		
		if(MinFileLength > FileLength)
			MinFileLength = FileLength;
		else if(MaxFileLength < FileLength)
			MaxFileLength = FileLength;	
	}
	
	public void setFileExtensions(String fileExtensions) {
		String[] temp = FileExtensions.split(";");
		for(int i=0;i< temp.length;i++){
			int j = 0;
			
			try {
				j = (Integer.parseInt(temp[i].substring(4,temp[i].length())));
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block

			}
			j++;
			if(fileExtensions.equals(temp[i].substring(0,3))){
				temp[i] = temp[i].substring(0,4) +j;
			}
		}
		FileExtensions = "";
		for(int i=0;i<temp.length;i++)
			FileExtensions += temp[i]+";";
	}
	
	
	
	public String FancyToString()
    {
        StringBuilder output = new StringBuilder();
        
        output.append("CrawlResult\n");
        
        output.append("Address: ");
        output.append(Address);
        output.append("\n");
        
        //output.append("HostName: ");
        //output.append(HostName);
        //output.append("\n");
        
        output.append("Agent: ");
        output.append(Agent);
        output.append("\n");
        
        output.append("Status: ");
        output.append(Status);
        output.append("\n");
        
        //output.append("Peers: ");
        //output.append(Peers);
        //output.append("\n");
        
        //output.append("Leaves: ");
        //output.append(Leaves);
        //output.append("\n");
        
        output.append("NumFiles: ");
        output.append(NumFiles);
        output.append("\n");
        
        output.append("AvgFileLength: ");
        output.append(AvgFileLength);
        output.append("\n");
        
        output.append("MinFileLength: ");
        output.append(MinFileLength);
        output.append("\n");
        
        output.append("MaxFileLength: ");
        output.append(MaxFileLength);
        output.append("\n");
        
        output.append("FileExtensions: ");
        output.append(FileExtensions);
        output.append("\n");
        
        return output.toString();
        
    }
    
    public boolean LoadFromString(String input)
    {
        String[] lines = input.split("\n");
        //if(lines.length != 12)
        if(lines.length != 9)
        {
            System.out.println("Incorrectly formatted CrawlResult (length = " + lines.length + "):\n" + input);
            return false;
        }
        
        try
        {
            /*Address = lines[1];
            HostName = lines[2];
            Agent = lines[3];
            Status = lines[4];
            Peers = lines[5];
            Leaves = lines[6];
            NumFiles = Integer.parseInt(lines[7]);
            AvgFileLength = Integer.parseInt(lines[8]);
            MinFileLength = Integer.parseInt(lines[9]);
            MaxFileLength = Integer.parseInt(lines[10]);
            FileExtensions = lines[11];*/
            Address = lines[1];
            Agent = lines[2];
            Status = lines[3];
            NumFiles = Integer.parseInt(lines[4]);
            AvgFileLength = Integer.parseInt(lines[5]);
            MinFileLength = Integer.parseInt(lines[6]);
            MaxFileLength = Integer.parseInt(lines[7]);
            FileExtensions = lines[8];
        }
        catch(Exception e)
        {
            System.out.println("Error parsing CrawlResult String");
            e.printStackTrace();
            return false;
        }
        
        return true;
    }
    
    @Override
    public String toString()
    {
        return WriteToString();
        //return FancyToString();
    }
    
    public String[] GetPeers()
    {
        if(Peers.length() <= 5)
        {
            return new String[]{};
        }
        String[] peers = Peers.split(",");
        return peers;
    }
    
    public String[] GetLeaves()
    {
        if(Leaves.length() <= 5)
        {
            return new String[]{};
        }
        String[] leaves = Leaves.split(",");
        return leaves;
    }   
    
    public void PrintHTMLResult(FileWriter f, String name)
    {
    
    	try {
    		
    		//f.write("<b>"+ this.HostName + " ("+this.Address+")</b>");
    		f.write("<b>"+ this.Address +"</b>");
    		//f.write("<br>Crawled at " + date + "<br>");
                
    		//Print out Ultra peers to html
                String[] peers = this.GetPeers();
    		f.write("<br><b>List of " +peers.length +" Ultra Peers:</b><br>");
    		f.write("<table border=\"1\" bordercolor=\"#FFCC00\" style=\"background-color:#FFFFCC\" width=\"400\" cellpadding=\"3\" cellspacing=\"3\"><tr>");
    		for(int i=0; i < peers.length; i++)
        	{	if(i%4 == 0){f.write("</tr><tr>");}
        			
    			String pageName = peers[i].replace('.', '_');
    			pageName = pageName.replace(':','_');
    			pageName = pageName + ".htm";
                        
    			f.write("<td><a href=\""+ pageName +"\">"+ peers[i] + "</a><td>");
                        
        		//f.write("<td>"+peers[i]+ "</td>");
 
        	}
                
    		//Print out Leaf peers to html
                String[] leaves = this.GetLeaves();
    		f.write("</table><br><br><br><b>List of " +leaves.length+ " Leaf Peers:</b><br>");
    		f.write("<table border=\"1\" bordercolor=\"#FFCC00\" style=\"background-color:#FFFFCC\" width=\"400\" cellpadding=\"3\" cellspacing=\"3\"><tr>");
    		for(int i=0; i < leaves.length; i++)
        	{	if(i%4 == 0){f.write("</tr><tr>");}
        			
    			String pageName = leaves[i].replace('.', '_');
    			pageName = pageName.replace(':','_');
    			pageName = pageName + ".htm";
                        
    			f.write("<td><a href=\""+ pageName +"\">"+ leaves[i] + "</a><td>");
                        
        		//f.write("<td>"+leaves[i]+"</td>");
 
        	}
    		f.write("</table>");
                
    		/*//Print out files to html
    		f.write("</table><br><br><br><b>List of " +this.GetFiles().size()+ " Files:</b><br>");
    		
    		Iterator<String> FilesIt = this.GetFiles().iterator();
    		f.write("<table border=\"1\" bordercolor=\"#FFCC00\" style=\"background-color:#FFFFCC\" width=\"400\" cellpadding=\"3\" cellspacing=\"3\"><tr>");
    		for(int i=0;FilesIt.hasNext();i++)
        	{	if(i>4){f.write("</tr><tr>");i=0;}
        			
        		f.write("<td>"+FilesIt.next()+"</td>");
 
        	}
    		f.write("</table>"); */   		
    		
    		
    		
    		
    		
    		f.flush();
    		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Creation of WWW/" + name +"failed");
			e.printStackTrace();
		}
    }
}
